// pages/my/history/list.js

const app = getApp();
const util = require("../../../utils/util");
const common = require("../../../utils/common");
const requestUtil = require("../../../utils/request");
const api = require("../../../config/api");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    CustomBar: app.globalData.CustomBar,
    hasNoMoreSeen: false, // 没有更多记录
    hasSeenList: false, // 判断是否有历史记录
    userType: 1,
    cardInfo: null,
    fyNum: 1, // 页码
    fySize: 10, // 每页展示数
    seenList: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let type = options.type;//时间类型 0全部 1当天
    let userType = app.globalData.userType;
    let cardInfo = userType == 1 ? app.globalData.employeeInfo : userType == 2 ? app.globalData.customInfo : null;
    this.setData({
      userType,
      cardInfo,
      type: type,
      typeName: common.formatVisitType(type)
    })
    this.getSeenList();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (!this.data.hasNoMoreSeen) {
      this.getSeenList();
    }
  },
  /**
   * 获取历史记录列表
   */
  getSeenList: async function (id) {
    let params = {
      userType: this.data.userType,
      fyNum: this.data.fyNum,
      fySize: this.data.fySize
    };
    if (this.data.userType == 1) {
      params.userId = this.data.cardInfo.id;
    } else {
      params.custId = this.data.cardInfo.id;
    }
    if("today" == this.data.type) {
        params.startDate = util.dateFormat(new Date(),'yyyy-MM-dd')
    } else if("tomonth" == this.data.type) {
        params.startDate = util.dateFormat(new Date(),'yyyy-MM') + '-01'
        params.endDate = util.dateFormat(new Date(),'yyyy-MM-dd')
    } else if("prevmonth" == this.data.type) {
        params.startDate = util.preMonthFirstDay(new Date())
        params.endDate = util.preMonthLastDay(new Date())
    }
    let res = await requestUtil.request("POST", api.urls.getHistoryList, params);
    if (res.data.code != 0) {
      util.showToast(res.data.msg);
      return;
    }
    let seenList = res.data.data.rows;
    let list = this.data.seenList;
    list.push(...seenList);
    
    let hasNoMore = this.data.hasNoMoreSeen;
    if(params.fyNum * params.fySize >= res.data.data.total) {
        hasNoMore = true;
    }
    this.setData({
      total: res.data.data.total,
      seenList: list,
      hasSeenList: true,
      hasNoMoreSeen: hasNoMore,
      fyNum: params.fyNum + 1
    })
  },
  /**
   * 打电话
   */
  onCall: function (e) {
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.dataset.phone,
    })
  },

})